package com.orangelife.service.propFeeUnit.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeUnit.IPropFeeUnitService;
import com.orangelife.service.propFeeUnit.model.PropFeeUnitDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeUnit类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-06-18 12:28:37
 * ==============================================================
 */
@Service("propFeeUnitService")
public class PropFeeUnitServiceImpl extends MybaitsGenericDaoSubLogic implements
		IPropFeeUnitService {
	private static Logger logger = Logger
			.getLogger(PropFeeUnitServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeUnitServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeUnit(PropFeeUnitDto propFeeUnit)
			throws AppBizException {

		Assert.notNull(propFeeUnit);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (StringUtils.isBlank(propFeeUnit.getUnitName())) {
			throw new AppBizException(CodeConst.BIS_CODE_68001, "单元号不能为空");
		}

		if (!StringUtils.isBlank(propFeeUnit.getUnitName())) {
			if (propFeeUnit.getUnitName().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "单元号长度超出");
			}
		}

		if (StringUtils.isBlank(propFeeUnit.getUnitID())) {
			propFeeUnit.setUnitID(IdUtil.getuuId());
		}
		propFeeUnit.setItemStat(TypeConst.FLAG_ON);
		propFeeUnit.setTscreate(new Date());
		propFeeUnit.setTsedit(new Date());
		return this.getSqlSession().insert("PropFeeUnitMapper.insertSelective",
				propFeeUnit);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeUnit(PropFeeUnitDto propFeeUnit)
			throws AppBizException {

		Assert.notNull(propFeeUnit);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeUnit.getUnitID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeUnitDto oldPropFeeUnit = this.findPropFeeUnitByID(propFeeUnit
				.getUnitID());

		if (StringUtils.isBlank(propFeeUnit.getUnitName())) {
			throw new AppBizException(CodeConst.BIS_CODE_68001, "单元号不能为空");
		}

		if (!StringUtils.isBlank(propFeeUnit.getUnitName())) {
			if (propFeeUnit.getUnitName().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "单元号长度超出");
			}
		}

		if (oldPropFeeUnit == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeeUnit.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeUnitMapper.updateByPrimaryKeySelective", propFeeUnit);
	}

	@Override
	public PropFeeUnitDto findPropFeeUnitByID(String propFeeUnitID)
			throws AppBizException {
		if (StringUtils.isBlank(propFeeUnitID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeUnitDto oldPropFeeUnit = this.getSqlSession().selectOne(
				"PropFeeUnitSearchMapper.selectByPrimaryKey", propFeeUnitID);
		if (oldPropFeeUnit == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeUnit.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeUnit;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeUnitByLogic(String propFeeUnitID)
			throws AppBizException {

		if (StringUtils.isBlank(propFeeUnitID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeUnitDto oldPropFeeUnit = this.findPropFeeUnitByID(propFeeUnitID);

		oldPropFeeUnit.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeeUnit.setTsedit(new Date());
		return this.getSqlSession()
				.update("PropFeeUnitMapper.updateByPrimaryKeySelective",
						oldPropFeeUnit);
	}

	@Override
	public List<Map<String, Object>> findPropFeeUnitByPage(String tsCreate,
			String search, int pageNo, int pageSize) throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeUnitSearchMapper.findAllPropFeeUnit", map);
		return list;
	}

	@Override
	public List<PropFeeUnitDto> getPropFeeUnitByPage(String tsCreate,
			String search, int pageNo, int pageSize) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeUnitDto> list = this.getSqlSession().selectList(
				"PropFeeUnitSearchMapper.getAllPropFeeUnitDto", map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeUnitByRelevantID(
			String unitID, String buildingID) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("unitID", unitID);
		map.put("buildingID", buildingID);

		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeUnitSearchMapper.selectPropFeeUnitDtoyRelevantID", map);
		return list;
	}

	@Override
	public List<PropFeeUnitDto> findPropFeeUnitByRelevantID(String unitID,
			String buildingID, String unitName) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("unitID", unitID);
		map.put("buildingID", buildingID);
		map.put("unitName", unitName);

		List<PropFeeUnitDto> list = this.getSqlSession().selectList(
				"PropFeeUnitSearchMapper.findPropFeeUnitDtoByRelevantID", map);
		return list;
	}

	@Override
	public List<PropFeeUnitDto> bauthfindPropFeeUnit(List<String> propFeeUnitIDs)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeUnitDto> list = this.getSqlSession().selectList(
				"PropFeeUnitSearchMapper.batchFindPropFeeUnit", propFeeUnitIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeUnitByMap(
			Map<String, Object> propFeeUnit) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeUnitSearchMapper.batchFindPropFeeUnitByMap",
				propFeeUnit);
		return list;
	}

	@Override
	public int addPropFeeUnit(List<PropFeeUnitDto> propFeeUnit)
			throws AppBizException {
		if (propFeeUnit != null && propFeeUnit.size() > 0) {
			return this.getSqlSession().insert(
					"PropFeeUnitBauthMapper.bauthInsert", propFeeUnit);
		}
		return 1;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int bauthDeleteUnits(List<String> unitIDs) throws AppBizException {
		return this.getSqlSession().update(
				"PropFeeUnitBauthMapper.bauthDeleteLg", unitIDs);
	}

}